/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-10 15:43:47
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-15 19:31:02
 */
/**
 * request 请求
 */

const appInst = getApp();
let token = '';
wx.getBackgroundFetchToken({
  success(res) {
    token = res.token
  }
})
class request {
  constructor() {
    this._base = 'http://172.16.1.40/app'
    // this._base = 'http://172.16.1.40/app'
    this._header = {
      'Content-Type': 'application/json',
    }
  }

  /**
   * 设置统一的异常处理
   */
  setErrorHandler(handler) {
    this._errorHandler = handler;
  }

  /**
   * GET类型的网络请求
   */
  get(url, data, header = this._header) {
    return this.requestAll(url, data, header, 'GET')
  }

  /**
   * POST类型的网络请求
   */
  post(url, data, header = this._header) {
    return this.requestAll(url, data, header, 'POST')
  }
  /**
   * DELETE类型的网络请求
   */
  delete(url, data, header = this._header) {
    return this.requestAll(url, data, header, 'DELETE')
  }
  /**
   * PUT类型的网络请求
   */
  put(url, data, header = this._header) {
    return this.requestAll(url, data, header, 'PUT')
  }
  wx_author(message) {
    wx.showToast({
      title: message,
      icon: 'none',
      duration: 800,
    })
    wx.navigateTo({
      url: '/pages/login/phone/index',
    })
  }
  /**
   * 网络请求
   */
  requestAll(url, data, header, method) {
    // 获取token
    header.token = wx.getStorageSync('token')
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    // 判断是否有授权登录

    return new Promise((resolve, reject) => {
      wx.request({
        url: this._base + url,
        data: data,
        header: header,
        method: method,
        success: (res => {
          if (res.statusCode === 403) {
            this.wx_author(res.data.message)
          }
          if (res.statusCode === 200) {
            //200: 服务端业务处理正常结束
            console.log('------------------------------------');
            console.log(res.data);
            console.log('------------------------------------');
            if (res.data.code === 403) {
              this.wx_author(res.data.message)
            }
            if (res.data.code === 200) {
              resolve(res.data)
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 800,
                mask: false,
              });
              reject(res.data.message)
            }
          } else {
            wx.showToast({
              title: res.message,
              icon: 'none',
              duration: 800,
              mask: false,
            });
            //其它错误，提示用户错误信息
            if (this._errorHandler != null) {
              //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理
              this._errorHandler(res)
            }
            reject(res)
          }
        }),
        fail: (res => {
          if (this._errorHandler != null) {
            this._errorHandler(res)
          }
          reject(res)
        }),
        // 加载完成
        complete: () => {
          wx.hideLoading();
          wx.hideNavigationBarLoading();
        }
      })
    })
  }
}

const _request = new request;
export {
  _request as request
};